package com.yuan.service.impl;

import com.yuan.entity.Flow;
import com.yuan.entity.Orders;
import com.yuan.mapper.OrdersMapper;
import com.yuan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 新增订单
     *
     * @param orders
     */
    @Override
    public void saveOrder(Orders orders) {
        //orders.setId(String.valueOf(System.currentTimeMillis()));
        orders.setOrderStatus("待付款");
        //orders.setOrderNum(orders.getId());
        orders.setCreateTime(new Date());
        ordersMapper.insertSelective(orders);
    }

    /**
     * 修改叮当状态，改为 支付成功，已付款; 同时新增支付流水
     *
     * @param orderId
     * @param flowNum
     * @param paidAmount
     */
    @Override
    public void updateOrderStatus(String orderId, String flowNum, String paidAmount) {
        Orders orders = ordersMapper.selectByPrimaryKey(orderId);
        if (orders != null) {
            orders.setOrderStatus("已付款");
            orders.setPaidAmount(paidAmount);
            orders.setPaidTime(new Date());
            Flow flow = new Flow();
            flow.setFlowNum(String.valueOf(System.currentTimeMillis()));
            flow.setOrderNum(orders.getOrderNum());
            flow.setProductId(orders.getProductId());
            flow.setPaidAmount(paidAmount);
            flow.setPaidMethod(1);
            flow.setBuyCounts(orders.getBuyCounts());
            flow.setCreateTime(new Date());
        }
    }

    /**
     * 获取订单
     *
     * @param orderId
     * @return
     */
    @Override
    public Orders getOrderById(String orderId) {
        return ordersMapper.selectByPrimaryKey(orderId);
    }
}
